<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>读取xlsx内容</title>
    <script src="xlsx.js"></script>
</head>
<body>
    <h1>前端导入Excel文件, 解析为json</h1>
    <div>选择你要解析的Excel文件</div>
    <input type="file" accept=".xlsx" onchange="upload(this.files)">
</body>
<script>
    function upload(files) {
        console.log(files[0]);
        let fileObj = files[0]
        const fileReader = new FileReader()     //FileReader是专门操作二进制数据的,主要将文件内容读入内存
        fileReader.readAsArrayBuffer(fileObj)   //读取Blob或File内容
        fileReader.onload = (event) => {
            const fileData = event.target.result
            const workbook = XLSX.read(fileData, { type: "binary" })    //从excel中提取数据
            const worksheetname = workbook.SheetNames[0]
            console.log(worksheetname);
            const sheetJson = XLSX.utils.sheet_to_json(workbook.Sheets[worksheetname])
            console.log(sheetJson);
        }
    }
</script>
</html>